package com.example.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.Mapper.XsbjDao;
import com.example.pojo.Customer;
import com.example.pojo.Materialzhu;
import com.example.pojo.Xs_bj;
import com.example.pojo.Xs_bjinfo;
import com.example.vo.CustVo;
import com.example.vo.LrygVo;
import com.example.vo.LsjyVo;
import com.example.vo.Matvo;
import com.example.vo.PageBean;
import com.example.vo.Xsbjinfovo;
import com.example.vo.Xsbjvo;

@Service
public class XsbjBiz {
@Autowired
private XsbjDao dao;
public XsbjDao getDao() {
	return dao;
}

public void setDao(XsbjDao dao) {
	this.dao = dao;
}
/**
 * 点击保存时，单据保存过则修改，没有则新增
 * @param bj
 */
public void insertbj(Xs_bj bj){
	if(dao.querycount(bj.getBillno())<=0){
		dao.addbj(bj);
	}else{
		dao.deletebjinfo(bj.getBillno());
		dao.updatebj(bj);
		for (Xs_bjinfo temp : bj.getBjinfo()) {
			temp.setBillno(bj.getBillno());
			dao.addbjinfo(temp);
		}
	}	
}

/**
 * 点击删除，删除报价单主详单
 * @param billno
 */
public void removebj(String billno){
	dao.deletebj(billno);
}

/**
 * 点击审核，修改报价单主单的状态
 * @param billno
 */
public void updatebjstatus(String billno){
	if(dao.querystatus(billno)==0){
		dao.updatestatus(billno);
	}else{
		throw new RuntimeException("该单已审核");
	}
	
}

/**
 * 点击反审核，修改报价单主单的状态
 * @param billno
 */
public void updatebjstatus2(String billno){
	if(dao.querystatus(billno)!=0){
		dao.updatestatus2(billno);
	}
	else{
		throw new RuntimeException("该单还未审核，不能进行反审核");
	}
}
/**
 * 分页
 * @param pagenum
 * @param pagesize
 * @return
 */
public PageBean queryallbypage(int pagenum,int pagesize){
	PageBean pb=new PageBean();
	pb.setPagenum(pagenum);
	pb.setPagesize(pagesize);
	pb.setRowcount(dao.queryallcount());
	List<Xsbjvo> vos=new ArrayList<Xsbjvo>();
	List<Xsbjinfovo> vo=new ArrayList<Xsbjinfovo>();
	
	for (Xs_bj bj: dao.queryBypage((pagenum-1)*pagesize, pagesize)) {
		List<Xs_bjinfo> info=dao.querybyid(bj.getBillno());
		bj.setBjinfo(info);
	  for (Xs_bjinfo bjinfo : bj.getBjinfo()) {
		  Xsbjinfovo vv=new Xsbjinfovo(bj.getBillno(),bjinfo.getProdId(),bjinfo.getSumqty(),
				  bjinfo.getBeforemoney(),bjinfo.getZheshu(),bjinfo.getPrice(),bjinfo.getMoney(),
				  bjinfo.getTaxlv(),bjinfo.getTaxmoney(),bjinfo.getSumtax(),bjinfo.getIspresent(),
				  bjinfo.getRemark(),bjinfo.getProname());
		  vo.add(vv);
	}
	  Xsbjvo oo=new Xsbjvo(bj.getBillno(),bj.getBilldate(),bj.getCustomerId(),
				bj.getFormalcast(),bj.getAddressId(),bj.getCustcurrency(),
				bj.getValiddate(),bj.getPriceoftax(),bj.getExchrate(),bj.getSalesId(),
				bj.getDepartId(), bj.getMaker(), bj.getPermitter(),bj.getShstatus(),
				bj.getTotalmoney(),vo);
	  vos.add(oo);
	}
	pb.setPagedata(vos);	
	return pb;
}

public String querymax(String billdates){
	String no=dao.querymax(billdates);
	if(no==""||no==null){
		no=billdates.replace("-", "")+"001";
	}else{
		no=String.valueOf(Long.valueOf(no)+1);
	}
	return no;
}

public List<Customer> findcust(){
	return dao.querycust();
}

/**
 * 根据客户id查询客户信息
 * @param cid
 * @return
 */
public CustVo querybycid(String cid){
	return dao.querybycid(cid);
}
/**
 * 查询所有的物料
 * @return
 */
public List<Materialzhu> querymat(){
	return dao.querymat();
}
/**
 * 根据物料id查询物料信息
 * @param mid
 * @return
 */
public Matvo querybymatid(String mid){
	Matvo vo=dao.querybymatid(mid);
	vo.setZheshu(100);
	return vo;
}

public List<LsjyVo> querybycust(String cust){
	List<LsjyVo> list = dao.querybycust(cust);
	for (LsjyVo item : list) {
		item.setDanbie("销售报价单");
		item.setBibie("RMB");
	}
	return list;
}

public List<LsjyVo> querybycust2(String cust){
	List<LsjyVo> list = dao.querybycust2(cust);
	for (LsjyVo item : list) {
		item.setDanbie("销售订单");
		item.setBibie("RMB");
	}
	return list;
}

public List<LsjyVo> querybycust3(String cust){
	List<LsjyVo> list = dao.querybycust3(cust);
	for (LsjyVo item : list) {
		item.setDanbie("销售出库单");
		item.setBibie("RMB");
	}
	return list;
}

public List<LrygVo> querybybill(String billno){
	return dao.querybybill(billno);
}
   
}
